/*
 * ntp_utils.c
 *
 *  Created on: 2022年1月28日
 *      Author: ljf
 */
#include "user_interface.h"
#include "osapi.h"
#include "espconn.h"
#include "os_type.h"
#include "mem.h"
#include "sntp.h"

#include "ntp_utils.h"

/*****************************************/
#define SECSPERMIN	60L
#define MINSPERHOUR	60L
#define HOURSPERDAY	24L
#define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
#define SECSPERDAY	(SECSPERHOUR * HOURSPERDAY)
#define DAYSPERWEEK	7
#define MONSPERYEAR	12

#define YEAR_BASE	1900
#define EPOCH_YEAR      1970
#define EPOCH_WDAY      4
#define EPOCH_YEARS_SINCE_LEAP 2
#define EPOCH_YEARS_SINCE_CENTURY 70
#define EPOCH_YEARS_SINCE_LEAP_CENTURY 370

static const int mon_lengths[2][12] = {
  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
} ;

static const int year_lengths[2] = {
  365,
  366
} ;

//char sntp__tzrule[2];

/*****************************************************************************/
// sntp_service init
//=============================================================================
void sntp_service_init()
{
	ip_addr_t * addr = (ip_addr_t *)os_zalloc(sizeof(ip_addr_t));

	sntp_setservername(0, "us.pool.ntp.org");
	sntp_setservername(1, "ntp.sjtu.edu.cn");

	ipaddr_aton("210.72.145.44", addr);			// 点分十进制 => 32位二进制
	sntp_setserver(2, addr);					// 服务器_2【IP地址】
	os_free(addr);								// 释放addr

	sntp_init();	// SNTP system API init
}

int get_current_time(char *timeStamp)
{
	u8 ret = 0;
	uint32 TimeStamp;
	struct tm tm_val;

	 //Get the timestamp(second) from 1970.01.01 00:00:00 GMT+8
	 //-----------------------------------------------------------------
	 TimeStamp = sntp_get_current_timestamp();
	 os_printf("TimeStamp: %d\n", TimeStamp);
	 if(TimeStamp == 0)
	 {
		 return -2;
	 }
	 memset(&tm_val,0x00,sizeof(tm_val));

	 datetime_format_transform((time_t *)&TimeStamp, &tm_val);

	 if(timeStamp == NULL && TimeStamp != 0)
	 {
		return -1;
	 }
	 os_memset(timeStamp, 0, 32);
	 ret = os_sprintf(timeStamp, "%04d-%02d-%02d %02d:%02d:%02d",
								tm_val.tm_year, tm_val.tm_mon,
								tm_val.tm_mday,tm_val.tm_hour,
								tm_val.tm_min,tm_val.tm_sec);
	 os_printf("datetime:%s\n", timeStamp);
	 return ret;
}

struct tm *
datetime_format_transform(time_t * tim_p ,struct tm *res)
{
  long days, rem;
  time_t lcltime;
  int i;
  int y;
  int yleap;
  const int *ip;

  /* base decision about std/dst time on current time */
  lcltime = *tim_p;

  days = ((long)lcltime) / SECSPERDAY;

  rem = ((long)lcltime) % SECSPERDAY;
  while (rem < 0)
  {
      rem += SECSPERDAY;
      --days;
  }
  while (rem >= SECSPERDAY)
    {
      rem -= SECSPERDAY;
      ++days;
    }

  /* compute hour, min, and sec */
  res->tm_hour = (int) (rem / SECSPERHOUR);
  rem %= SECSPERHOUR;
  res->tm_min = (int) (rem / SECSPERMIN);
  res->tm_sec = (int) (rem % SECSPERMIN);

  /* compute day of week */
  if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
    res->tm_wday += DAYSPERWEEK;

  /* compute year & day of year */
  y = EPOCH_YEAR;
  if (days >= 0)
  {
    for (;;)
	{
	  yleap = isleap(y);
	  if (days < year_lengths[yleap])
	    break;
	  y++;
	  days -= year_lengths[yleap];
	}
  }
  else
  {
    do
	{
	  --y;
	  yleap = isleap(y);
	  days += year_lengths[yleap];
	} while (days < 0);
  }

  res->tm_year = y;
  res->tm_yday = days;
  ip = mon_lengths[yleap];
  for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
	  days -= ip[res->tm_mon];
  res->tm_mon += 1; //set tm_mon start by 1
  res->tm_mday = days + 1;

  return (res);
}
